package com.one.consumer.reserve.ui.adapter;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.support.annotation.StringRes;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;


import com.one.consumer.R;

import org.xutils.image.ImageOptions;
import org.xutils.x;

public class SmartViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    private final AdapterView.OnItemClickListener mListener;

    public SmartViewHolder(View itemView, AdapterView.OnItemClickListener mListener) {
        super(itemView);
        this.mListener = mListener;
        itemView.setOnClickListener(this);

        /**
         * 设置水波纹背景
         */
        if (itemView.getBackground() == null) {
            TypedValue typedValue = new TypedValue();
            Resources.Theme theme = itemView.getContext().getTheme();
            int top = itemView.getPaddingTop();
            int bottom = itemView.getPaddingBottom();
            int left = itemView.getPaddingLeft();
            int right = itemView.getPaddingRight();
            if (theme.resolveAttribute(android.R.attr.selectableItemBackground, typedValue, true)) {
                itemView.setBackgroundResource(typedValue.resourceId);
            }
            itemView.setPadding(left, top, right, bottom);
        }
    }

    @Override
    public void onClick(View v) {
        if (mListener != null) {
            int position = getAdapterPosition();
            if(position >= 0){
                mListener.onItemClick(null, v, position, getItemId());
            }
        }
    }

    private View findViewById(int id) {
        return id == 0 ? itemView : itemView.findViewById(id);
    }

    public SmartViewHolder text(int id, CharSequence sequence) {
        View view = findViewById(id);
        if (view instanceof TextView) {
            ((TextView) view).setText(sequence);
        }
        return this;
    }

    public SmartViewHolder text(int id,@StringRes int stringRes) {
        View view = findViewById(id);
        if (view instanceof TextView) {
            ((TextView) view).setText(stringRes);
        }
        return this;
    }

    public SmartViewHolder text(int id, String text) {
        View view = findViewById(id);
        if (view instanceof TextView) {
            ((TextView) view).setText(text);
        }
        return this;
    }

    public SmartViewHolder setClick(int id, View.OnClickListener listener, Object tag) {
        View view = findViewById(id);
       view.setOnClickListener(listener);
       view.setTag(tag);
        return this;
    }

    public SmartViewHolder setVisibaled(int id, int visibale) {
        View view = findViewById(id);
        view.setVisibility(visibale);
        return this;
    }

//    public RecyclerView text(int id) {
//        View view = findViewById(id);
//        if (view instanceof RecyclerView) {
//            return (RecyclerView)view;
//        }
//        return null;
//    }

    public SmartViewHolder textColorId(int id, int colorId) {
        View view = findViewById(id);
        if (view instanceof TextView) {
            ((TextView) view).setTextColor(ContextCompat.getColor(view.getContext(), colorId));
        }
        return this;
    }

    public SmartViewHolder image(int id, String url){
        View view = findViewById(id);
        if (view instanceof ImageView) {
//            ((ImageView) view).setImageResource(imageId);
            ImageOptions options = new ImageOptions.Builder()
                    .setConfig(Bitmap.Config.RGB_565)//设置图片质量,这个是默认的
                    .setFadeIn(true)//淡入效果
                    .setFailureDrawableId(R.mipmap.img_fail)
                    .setLoadingDrawableId(R.mipmap.img_loading)
//                    .setImageScaleType(ImageView.ScaleType.CENTER_CROP)
                    .build();
            x.image().bind((ImageView) view, url, options);
        }
        return this;
    }

    public SmartViewHolder setBackGround(int id, int imageId) {
        View view = findViewById(id);
//        if (view instanceof ImageView) {
            view.setBackgroundResource(imageId);
//        }
        return this;
    }
}